package com.primaryedu.ui.course;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.primaryedu.R;
import com.primaryedu.adapter.CommentAdapter;
import com.primaryedu.bean.CommentBean;
import com.primaryedu.bean.CourseBean;
import com.primaryedu.data.Contants;
import com.primaryedu.ui.BaseActivity;
import com.primaryedu.ui.exercise.CommentActivity;
import com.primaryedu.ui.exercise.CommentWriteActivity;
import com.primaryedu.widget.MyListView;
import com.primaryedu.widget.MyVideoView;

/**
 * 微课详情
 *
 * @author jun.wang
 * @description
 * @data 2017年1月4日
 */
public class CourseDetailActivity extends BaseActivity implements OnClickListener {
    List<CommentBean> list;
    CommentAdapter adapter;
    private String newId = "";
    private String type = "";
    private String videoUrl = "";

    private MyListView listView;
    private MyVideoView vv_player;
    MediaController mc;
    CourseBean courseBean;

    private TextView text_title, text_content, text_teacher, text_look;

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
                case 1001:
                    setData();
                    break;

                case 1002:
                    text_look.setText(list.size() + "");
                    break;

                default:
                    break;
            }

        }

        ;
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_course);

        newId = getIntent().getStringExtra("newId");
        type = getIntent().getStringExtra("type");
        videoUrl = getIntent().getStringExtra("videoUrl");
        init();

    }

    private void init() {
        list = new ArrayList<>();
        courseBean = new CourseBean();
        listView = (MyListView) this.findViewById(R.id.listView);
//		findViewById(R.id.text_write).setOnClickListener(this);

//		findViewById(R.id.text_look).setOnClickListener(this);
        findViewById(R.id.back).setOnClickListener(this);
        text_title = (TextView) this.findViewById(R.id.text_title);
        text_content = (TextView) this.findViewById(R.id.text_content);
        text_teacher = (TextView) this.findViewById(R.id.text_teacher);
        text_look = (TextView) this.findViewById(R.id.text_look);
        vv_player = (MyVideoView) findViewById(R.id.videoView);
        MediaController mc = new MediaController(this);
        vv_player.setMediaController(mc);

//		 Uri uri = Uri.parse(GlobalData.videoBeautyLists.get(0).getVideoUrl());
//		 vv_player.setVideoURI(uri);
//		 vv_player.setMediaController(new MediaController(CourseDetailActivity.this));
//		 vv_player.requestFocus();

        requestMicrocourseData();
    }

    @Override
    protected int BindLayout() {
        return R.layout.activity_course;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();

        getPageListJson();
    }

    private void setData() {
        System.out.println("videoUrl===" + Contants.course_api + videoUrl);

        Uri uri = Uri.parse(Contants.course_api + videoUrl);
        vv_player.setVideoURI(uri);
        vv_player.setMediaController(new MediaController(CourseDetailActivity.this));
        vv_player.requestFocus();
        vv_player.start();

        text_title.setText(courseBean.getTitle() + "");
        text_content.setText(courseBean.getContent() + "");
        text_teacher.setText(courseBean.getTeachername());

    }

    private void requestMicrocourseData() {
        StringBuilder builder = new StringBuilder();
        builder.append(Contants.getMicrocourse);
        builder.append("?id=").append(newId);

        StringRequest stringRequest = new StringRequest(Request.Method.GET,
                builder.toString(), new Response.Listener<String>() {
            @Override
            public void onResponse(String arg0) {

                try {

                    System.out.println("getMicrocourse===" + arg0.toString());

                    JSONObject json = new JSONObject(arg0);
                    String reason = json.getString("reason");
                    int result = json.getInt("result");
                    String data = json.getString("data");

                    if (result == 1) {
                        courseBean = new Gson().fromJson(data,
                                new TypeToken<CourseBean>() {
                                }.getType());
                        System.out.println("getMicrocourse==" + courseBean.toString());
                        handler.sendEmptyMessage(1001);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {
            }
        });
        //为此get请求设置一个Tag属性
        stringRequest.setTag("GET_TAG");
        //将此get请求加入
        requestQueue.add(stringRequest);
    }

    private void getPageListJson() {
        showDialog();
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Contants.getComment,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        System.out.println("getComment==" + response);
                        closeDialog();
                        try {
                            JSONObject json = new JSONObject(response);
                            int result = json.getInt("result");
                            String reason = json.getString("reason");

                            if (result == 1) {
                                String data = json.getString("data");
                                JSONObject dataObj = new JSONObject(data);
                                String listStr = dataObj.getString("list");

                                list = new Gson().fromJson(listStr,
                                        new TypeToken<List<CommentBean>>() {
                                        }.getType());
                                System.out.println("getComment=00=" + list.toString());

                                if (null != list && list.size() != 0) {
                                    handler.sendEmptyMessage(1002);
                                    adapter = new CommentAdapter(CourseDetailActivity.this, list);
                                    listView.setAdapter(adapter);
                                }

                            } else {
                                showToast(reason);
                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                closeDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                //在这里设置需要post的参数
                Map<String, String> map = new HashMap<String, String>();
                map.put("objectid", newId);
                map.put("type", type);
                map.put("pageNo", "1");
                map.put("pageSize", "10");
                return map;
            }
        };
        requestQueue.add(stringRequest);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;

        switch (v.getId()) {
            case R.id.back:
                finish();
                break;

            case R.id.text_write:
                intent = new Intent(CourseDetailActivity.this, CommentWriteActivity.class);
                intent.putExtra("newId", newId);
                intent.putExtra("type", "1");   // 视频
                startActivity(intent);
                break;

            case R.id.text_look:
                intent = new Intent(CourseDetailActivity.this, CommentActivity.class);
                intent.putExtra("newId", newId);
                intent.putExtra("type", "1");   // 视频
                startActivity(intent);
                break;
        }
    }


}
